package com.martin.lib_base.utils

import com.martin.lib_base.base.BaseResponse
import com.martin.lib_base.bean.UploadBean
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.net.NetApply
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

/**
 * @author：Houge
 * @date：2021/12/14 8:44
 */
object UploadUtil {

    suspend fun upload(path: String): BaseResponse<UploadBean> {
        loge("上传的文件: $path")
        val file = File(path)
        val body = file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
        val part = MultipartBody.Part.createFormData("file", file.name, body)
        return NetApply.api.uploadFile(
            part,
            mutableMapOf(
                "fileName" to file.name.toRequestBody("multipart/form-data".toMediaTypeOrNull()),
            )
        )
    }

}